package org.jeecg.modules.job.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.job.sub.entity.*;
import org.jeecg.modules.job.sub.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 *
 */
@RestController
@RequestMapping("/api/sub")
@Api(tags = "移动端答题管理模块")
@Slf4j
public class SubController {
    @Autowired
    private ISubPhaseService subPhaseService;
    @Autowired
    private ISubPhaseTitleService subPhaseTitleService;
    @Autowired
    private ISubPhaseRecordsService phaseRecordsService;
    @Autowired
    private ISubTitleService subTitleService;
    @Autowired
	private ISubSelectService subSelectService;
    @Autowired
	private ISubAnswerService answerService;

    /**
     * 查询答题主题列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "查询答题主题列表")
    @GetMapping(value = "/getPhaseList")
    public Result<IPage<SubPhase>> getPhaseList(SubPhase subPhase,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "20") Integer pageSize,
                                                HttpServletRequest req) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<SubPhase> queryWrapper = new QueryWrapper<>(subPhase);
        queryWrapper.orderByDesc("create_time");
        Page<SubPhase> page = new Page<SubPhase>(pageNo, pageSize);
        IPage<SubPhase> pageList = subPhaseService.page(page, queryWrapper);
//        BaseConfig config = configService.getBaseConfig();
        pageList.getRecords().forEach(item -> {
            long count = subPhaseTitleService.count(new QueryWrapper<>(new SubPhaseTitle().setPhaseId(item.getId())));
            item.setNumber((int) count);
//            item.setIntegral((int) count * config.getQuestionIntegral());
            //是否答题过
            SubPhaseRecords records = phaseRecordsService.getPhaseRecord(user.getId(), item.getId());
            if (records != null) {
                item.setIfAnswered(true);
            } else {
                item.setIfAnswered(false);
            }
            //判断是否已结束
            if (new Date().compareTo(item.getStartTime()) > 0 && new Date().compareTo(item.getEndTime()) < 0) {
                item.setIfRunning(true);
            } else {
                item.setIfRunning(false);
            }
        });
        return Result.OK(pageList);
    }


    /**
     * 查询题目列表
     * @param phaseId 阶段id
     * @return
     */
    @ApiOperation(value = "查询题目列表")
    @GetMapping(value = "/getTitleList")
    public Result<Object> getPhaseList(@RequestParam("phaseId") String phaseId) {
		List<SubPhaseTitle> list=subPhaseTitleService.getPhaseTitleList(new SubPhaseTitle().setPhaseId(phaseId));
		list.forEach(item->{
			item.setSubSelectList(subSelectService.list(new QueryWrapper<>(new SubSelect().setTitleId(item.getTitleId()))));
		});
        return Result.OK(list);
    }


	/**
	 * 答题
	 * @param answer
	 * @return
	 */
	@ApiOperation("答题")
	@PostMapping(value = "/answerQuestion")
	public Result<Object> answerQuestion(@RequestBody SubAnswer answer) {
		if(oConvertUtils.isEmpty(answer.getAnswer())){
			return Result.error("答案不能为空");
		}
		if(oConvertUtils.isEmpty(answer.getPhaseId())||oConvertUtils.isEmpty(answer.getTitleId())){
			return Result.error("参数错误");
		}
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		answer.setUserId(user.getId());
		try {
			boolean result=answerService.answerQuestion(answer);
			return Result.ok(result);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(e.getMessage());
		}
	}

	/**
	 * 查询题目答案
	 * @param titleId 题目id
	 * @return
	 */
	@ApiOperation(value = "查询题目列表")
	@GetMapping(value = "/getTitleAnswer")
	public Result<Object> getTitleAnswer(@RequestParam("titleId") String titleId) {
		SubAnswer result=answerService.getOne(new QueryWrapper<>(new SubAnswer().setTitleId(titleId)));
		return Result.OK(result);
	}


	/**
	 * 查询答题结果
	 * @param phaseId 阶段id
	 * @return
	 */
	@ApiOperation(value = "查询答题结果")
	@GetMapping(value = "/getPhaseRecord")
	public Result<Object> getPhaseRecord(@RequestParam("phaseId") String phaseId) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		SubPhaseRecords result=phaseRecordsService.getPhaseRecord(user.getId(),phaseId);
		return Result.OK(result);
	}

}